﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM.Admin.Page_Admin
{
    public partial class Answers : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["session"] == "new")
            {
                Session["question"] = null;
            }
            if (Session["question"] != null)
            {
                DTO.QuestionInfo queinf = new DTO.QuestionInfo();
                queinf = (DTO.QuestionInfo)Session["question"];
                LoadQuestion(queinf);
            }
        }

        protected void LoadQuestion(DTO.QuestionInfo inf)
        {
            //BUS.QuestionControl quectr = new BUS.QuestionControl();
            DataTable dt = inf.ToDataTable();
            dt.Rows.Add(inf.ToDataRow(dt));
            cbbQuestion.DataSource = dt;
            cbbQuestion.DataTextField = "Description";
            cbbQuestion.DataValueField = "ID";
            cbbQuestion.SelectedValue = inf.ID.ToString();
            cbbQuestion.DataBind();
            txtQuest.Text = inf.Description;
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.AnswerInfo ansInf = new DTO.AnswerInfo();
            ansInf = GetDataFromForm(ansInf);
            BUS.AnswerControl ctr = new BUS.AnswerControl();
            if (txtID.Text != "" && txtID.Text != "-1")
            {
                sErr = ctr.Update(ansInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Update successfull";
            }
            else
            {
                txtID.Text = ctr.Add(ansInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Insert successfull";
            }
            if (sErr == "")
                dgvList.Rebind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString();
                    BUS.AnswerControl ctr = new BUS.AnswerControl();
                    sErr = ctr.Delete(Convert.ToInt64(id));
                }
            }
            dgvList.Rebind();
        }

        private void RefreshForm(string val)
        {
            txtID.Text = val;
            txtDescription.Text = val;
            txtExplanation.Text = val;
            txtTextanswer.Text = val;
            txtOrder_Answer.Text = val;

            cbIsRight.Checked = false;
            rdlEnabled.SelectedIndex = 0;
            lbErr.Text = val;

        }

        private DTO.AnswerInfo GetDataFromForm(DTO.AnswerInfo ansInf)
        {
            if (txtID.Text != "")
                ansInf.ID = Convert.ToInt64(txtID.Text);
            ansInf.Question_ID = Convert.ToInt64(cbbQuestion.SelectedValue);
            ansInf.Description = txtDescription.Text;
            ansInf.Explanation = txtExplanation.Text;
            ansInf.Text_Answer = txtTextanswer.Text;


            if (cbIsRight.Checked)
            {
                ansInf.IsRight = true;
            }
            else
                ansInf.IsRight = false;



            if (rdlEnabled.SelectedValue == "1")
            {
                ansInf.Enabled = true;
            }
            else
                ansInf.Enabled = false;


            return ansInf;
        }

        private void SetDataToForm(DTO.AnswerInfo ansInf)
        {
            txtID.Text = ansInf.ID.ToString();
            cbbQuestion.SelectedValue = ansInf.Question_ID.ToString();
            txtDescription.Text = ansInf.Description;
            txtExplanation.Text = ansInf.Explanation;
            txtTextanswer.Text = ansInf.Text_Answer;
            txtOrder_Answer.Text = ansInf.Order_Answer;

            if (ansInf.IsRight == true)
            {
                cbIsRight.Checked = true;

            }
            else
                cbIsRight.Checked = false;


            if (ansInf.Enabled == true)
            {
                rdlEnabled.SelectedIndex = 0;
            }
            else
                rdlEnabled.SelectedIndex = 1;
        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                BUS.AnswerControl ctr = new BUS.AnswerControl();
                SetDataToForm(ctr.Get(Convert.ToInt64(id), ref sErr));
                lbErr.Text = "";
            }

        }

        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            if (Session["question"] != null)
            {
                DTO.QuestionInfo queinf = new DTO.QuestionInfo();
                queinf = (DTO.QuestionInfo)Session["question"];
                BUS.AnswerControl ctr = new BUS.AnswerControl();
                dgvList.DataSource = ctr.Get_ByIDQuestion(queinf.ID, ref sErr);
            }
            else
            {
                BUS.AnswerControl ctr = new BUS.AnswerControl();
                dgvList.DataSource = ctr.GetAll(ref sErr);
            }
        }

        protected void btnQuest_Click(object sender, EventArgs e)
        {
            Response.Redirect("Question.aspx");
        }



    }
}
